package com.db.dbdocument.generateDB.controller;


import com.db.dbdocument.generateDB.service.IDbInfoService;
import com.db.dbdocument.generateDB.vo.DbInfoVo;
import com.db.dbdocument.generateDB.vo.DbTableDocumentVo;
import com.db.dbdocument.generateDB.vo.DbTableInfoVo;
import com.db.dbdocument.generateDB.vo.DbTableStructureInfoVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dbInfo")
public class DbInfoController {

    @Resource
    private IDbInfoService dbInfoService;

    /**
     * 获取数据库信息 使用jdbc
     */
    @GetMapping("/getDbInfo")
    public DbInfoVo getDbInfo() throws SQLException, ClassNotFoundException {
        DbInfoVo dbInfo = dbInfoService.getDbInfo();
        return dbInfo;
    }

    /**
     * 获取数据库表信息 使用jdbc
     */
    @GetMapping("/getDbTableInfo")
    public List<DbTableInfoVo> getDbTableInfo() throws SQLException, ClassNotFoundException, ParseException {
        List<DbTableInfoVo> dbTableInfoVoList = dbInfoService.getDbTableInfo();
        return dbTableInfoVoList;
    }

    /**
     * 获取数据库表结构信息 使用jdbc
     */
    @GetMapping("/getDbTableStructureInfo")
    public List<DbTableStructureInfoVo> getDbTableStructureInfo() throws SQLException, ClassNotFoundException, ParseException {
        List<DbTableStructureInfoVo> dbTableStructureInfoList = dbInfoService.getDbTableStructureInfo();
        return dbTableStructureInfoList;
    }


    /**
     * 获取数据库表文档 使用jdbc
     */
    @GetMapping("/getDbTableDocument")
    public DbTableDocumentVo getDbTableDocument() throws SQLException, ParseException, ClassNotFoundException {
        DbInfoVo dbInfo = dbInfoService.getDbInfo();
        List<DbTableInfoVo> dbTableInfoVoList = dbInfoService.getDbTableInfo();
        List<DbTableStructureInfoVo> dbTableStructureInfoList = dbInfoService.getDbTableStructureInfo();

        dbTableInfoVoList.stream().map(item->{
            item.setDbTableStructureVoList(dbTableStructureInfoList.stream().filter(filter -> filter.getTableName().equals(item.getTableName())).collect(Collectors.toList()));
            return item;
        }).collect(Collectors.toList());

        DbTableDocumentVo dbTableDocumentVo = new DbTableDocumentVo();
        dbTableDocumentVo.setDbInfoVo(dbInfo);
        dbTableDocumentVo.setDbTableInfoVoList(dbTableInfoVoList);

        return dbTableDocumentVo;
    }

    /**
     * 获取数据库表文档 使用数据源
     */
    @GetMapping("/getDataSoreceTableInfo")
    public DbTableDocumentVo getDataSoreceTableInfo() throws SQLException, IOException, ClassNotFoundException {
        DbTableDocumentVo dataSoreceTableInfo = dbInfoService.getDataSoreceTableInfo();

        return dataSoreceTableInfo;
    }

}
